package com.test.account.util;

import com.test.account.dto.Page;
import com.test.account.pojo.Account;
import com.test.account.service.AccountService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by 57695 on 2017/3/13.
 */
@Repository
public class DataTable {

    @Autowired
    private AccountService accountService;

    private List<Account> results = new ArrayList<Account>();

    public List<Account> loadAccountList(int pageSize, int startRecord, String sortColumn, String sortDir, String searchValue) {

        StringBuffer sql = new StringBuffer("select * from account ");

        // search
        String[] columnsName = { "ac_code", "ac_name", "ac_createtime","ac_type" };
        boolean searchAble = false;
        if(searchValue != null && !"".equals(searchValue)) {
            sql.append("where ");
            searchAble = true;
        }

        if(searchAble) {
            StringBuffer temp = new StringBuffer();
            for (String column : columnsName) {
                temp.append( column+ " like '%" + searchValue + "%' or ");
            }
            sql.append(temp.substring(0, temp.length() - 3));
        }

        // order
        sql.append(" order by " + sortColumn + " " + sortDir + " ");

        // pagination
        sql.append(" limit "+startRecord+","+pageSize);
        System.out.println(sql.toString());
        results = accountService.ExecuteSql(sql.toString());
        return results;
    }

    public int getRecord(String searchValue) {

        StringBuffer sql = new StringBuffer("select * from account ");

        // search
        String[] columnsName = { "ac_code", "ac_name", "ac_createtime","ac_type" };
        boolean searchAble = false;
        if(searchValue != null && !"".equals(searchValue)) {
            sql.append("where ");
            searchAble = true;
        }

        if(searchAble) {
            StringBuffer temp = new StringBuffer();
            for (String column : columnsName) {
                temp.append( column+ " like '%" + searchValue + "%' or ");
            }
            sql.append(temp.substring(0, temp.length() - 3));
        }

        results = accountService.ExecuteSql(sql.toString());
        return results.size();
    }

}
